package com.dmbjz.demo.config;


import com.dmbjz.demo.pojo.Student;
import com.dmbjz.demo.pojo.User;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.context.annotation.Bean;

//放在类上，只有条件生效，类才生效；放在方法上，只有条件生效，方法才生效
@ConditionalOnClass(name = "com.alibaba.druid.FastsqlException")
//声明是配置类
@SpringBootConfiguration
public class AppConfig2 {

    //如果容器中有 com.alibaba.druid.FastsqlException 类，则注入 miUser
    @ConditionalOnClass(name = "com.alibaba.druid.FastsqlException")
    @Bean
    public User miUser(){
        var user = new User();
        user.setName("mi").setAge(18);
        return user;
    }

    //如果容器中没有 com.alibaba.druid.FastsqlException 类，则注入 vivoStudent
    @ConditionalOnMissingClass(value = "com.alibaba.druid.FastsqlException")
    @Bean
    public Student vivoStudent(){
        var student = new Student();
        student.setName("vivo").setAge(18);
        return student;
    }

    //如果容器中有 Student 类型的 Bean，则进行注入 oppoStudent
    @ConditionalOnBean(value = Student.class)
    @Bean
    public Student oppoStudent(){
        var student = new Student();
        student.setName("oppo").setAge(18);
        return student;
    }

    //如果容器中没有 Student 类型的 Bean，则进行注入 relameUser
    @ConditionalOnMissingBean(value = Student.class)
    @Bean
    public User relameUser(){
        var temp = new User();
        temp.setName("Relame").setAge(18);
        return temp;
    }

}
